package com.yanqu.road.server.cmd.beautyescort;

import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.pb.beautyescort.BeautyEscortProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.beautyescort.CrossBeautyEscortMgr;
import com.yanqu.road.server.manager.beautyescort.CrossEscortPlayerWrap;
import com.yanqu.road.server.manager.user.CrossUserMgr;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.Protocol;

@Cmd(code = Protocol.C_BEAUTY_ESCORT_GET_CLIENT, desc = "走镖主页-委托人")
public class CrossBeautyEscortClientCmd extends CrossCommand {

    UserBaseInfo userBaseInfo;
    long totalGlamour;
    int unionLv;
    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        BeautyEscortProto.CrossBeautyEscortClientReqMsg reqMsg = BeautyEscortProto.CrossBeautyEscortClientReqMsg.parseFrom(packet.getBytes());
        userBaseInfo = PlayerBasePb.parseToUserBaseInfo(reqMsg.getUserInfo());
        totalGlamour = reqMsg.getTotalGlamour();
        unionLv = reqMsg.getUnionLv();
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        CrossUserMgr.updateUserBaseInfo(packet.getPlayerId(), userBaseInfo);

        CrossEscortPlayerWrap escortPlayer = CrossBeautyEscortMgr.getEscortPlayer(serverId, packet.getPlayerId());
        if(escortPlayer == null){
            escortPlayer = CrossBeautyEscortMgr.initEscortPlayer(serverId, packet.getPlayerId(), userBaseInfo);
        }else {
            escortPlayer.setUserBaseInfo(userBaseInfo);
        }
        escortPlayer.getUserData().setTotalGlamour(totalGlamour);
        escortPlayer.getUserData().setUnionLv(unionLv);
        escortPlayer.randomClient();
        BeautyEscortProto.BeautyEscortClientRespMsg.Builder respMsg = BeautyEscortProto.BeautyEscortClientRespMsg.newBuilder();
        respMsg.setRet(0);
        respMsg.addAllClient(escortPlayer.getUserData().getClientList());
        YanQuMessage pbMsg = YanQuMessageUtils.buildMessage(Protocol.U_BEAUTY_ESCORT_GET_CLIENT, respMsg);
        MessageHelper.sendPacket(serverId, packet.getPlayerId(), pbMsg);
    }

    @Override
    public void sendData() {

    }
}
